﻿using System;
using System.Web.UI.WebControls;
using AgencyEntityLib.BaseEntity;
using OutdoorLib.BaseEntity.WS;
using OutdoorLib.BaseEntity.Dictionary;
using OutdoorLib.BaseEntity.Agency;
using System.Collections.Generic;
using Grishko.DataTier;

using Grishko.UI;
using System.Web.UI;


public partial class SysDictionary_Frame : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        tvDictionary.BuildNode += new TreeControl._BuildNode(tvDictionary_BuildNode);
        if (!IsPostBack)
        {         
            MyBuildTree();
        }        
    }



    private void MyBuildTree()
    {
        DictionaryTree.Nodes.Clear();
        TreeNode chnd = new TreeNode();
        chnd.Text = "<a class=TreeNode  ID=DeprtmentTree href=\"#\"  onclick=\"showTreeBranch('iFrameDictionary_TreeDictionaryEditor.aspx?TypeID=" + "DepartmentTree" + "');\">  Орг. структура</a>";
        DictionaryTree.Nodes.Add(chnd);


        chnd = new TreeNode();
        chnd.Text = "<a class=TreeNode ID=dSiteMap_  href=\"#\"  onclick=\"showTreeBranch('iFarmeAdmin_SiteMap.aspx','dSiteMap_');\"> Карта сайта </a>";
        DictionaryTree.Nodes.Add(chnd);


        tvDictionary.DataSource = dsDictionaryTree;
        tvDictionary.DataBind();

    }

    protected Control tvDictionary_BuildNode(TreeControl tree, TreeEntitySingle nd)
    {
        LinkButton btn = new LinkButton();
        btn.ID = nd.ID;
        btn.CssClass = tree.CssNode;
        btn.Text = (nd as DictionaryItem).Value.ToString();        
        string[] param = {nd.ID,tree .CssNode,tree.CssSelectedNode};

        if(nd.ID.Length ==36)                
            btn.Attributes.Add("onClick", String.Format("onSelectBranch('iFrameDictionary_TreeDictionaryEditor.aspx?TypeID={0}','{1}','{2}','{0}');", param));

        if(nd.ID ==TreeDictionaryItem .TreeDictionary_AdressProgram )
            btn.Attributes.Add("onClick", String.Format("onSelectBranch('iFarmeDictionary_AdressPrograms.aspx','{1}','{2}','{0}');", param));
        return btn;
    }

    private TreeDictionaryItem dsDictionaryTree
    {
        get
        {
            TreeDictionaryItem result = new TreeDictionaryItem() {ID="DictionaryRoot", Name = "Справочники" };
            TreeDictionaryItem.BranchItem br = new TreeDictionaryItem.BranchItem() { ParamID = "ProviderDictionary", Value = "Системные" };
            FillTreeBranch(AppEnvironment.DictionarySource.GetProviderDictionaryCategoryList(), br);
            result.ChildNodes.Add(br);


            br = new TreeDictionaryItem.BranchItem() { ParamID = "AgencyDictionary", Value = "Пользовательские" };
            FillTreeBranch(AppEnvironment.DictionarySource.GetAgencyDictionaryCategoryList( ), br);
            result.ChildNodes.Add(br);

            return result;

        }

    }

    private void FillTreeBranch(List<DictionaryCategoryItem> List, TreeDictionaryItem.BranchItem Parent)
    {
        foreach (DictionaryCategoryItem i in List)        
        {
            TreeDictionaryItem.BranchItem br = new TreeDictionaryItem.BranchItem() { ParamID = i.ID , Value = i.Name };
            Parent.ChildNodes.Add(br);

        }
        

        
    }

    
}
